package com.wy.business.market.home.actions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.wy.business.market.home.services.ContentService;
import com.wy.framework.actions.AbstractBaseAction;
import com.wy.framework.dao.ErrorSqlException;


/**
 * 项目名称：openportal
 * 类名称：ContentAction
 * 类描述：
 * 创建人：田松林
 * 创建时间：2016-1-8 上午11:46:44
 * 修改人：田松林
 * 修改时间：2016-1-8 上午11:46:44
 * 版本信息：@version 1.0
 * Copyright (c) 2016万雍科技（上海）有限公司-版权所有  
 * 修改备注：
 * @version 
 */
@ParentPackage("json")
@Namespace("/home/content")
public class ContentAction extends AbstractBaseAction {
	
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private ContentService contentService;
	
	private List<Map> rows;
	
	/**
	 * Function Name               homeSearch                                   
	 * @return
	 * @throws ErrorSqlException          		             
	 * @description (全局搜索)
	 * Modify History:              Date             Programmer       Notes
	 *                            ---------        ---------------  ---------
	 *                      2016-1-8 上午11:46:51           田松林                   Initial
	 **********************************************************************
	 */
	@Action(value = "search", results = { @Result(name = "success", location = "/jsps/apiEncrypt.jsp") })
	public String homeSearch() throws ErrorSqlException{
		String searchContent=ServletActionContext.getRequest().getParameter("searchContent");
		String userId=ServletActionContext.getRequest().getParameter("userId");
		rows=new ArrayList<Map>();
		if(!StringUtils.isBlank(searchContent)){
			rows=contentService.querySearchResult(userId, searchContent);
		}
		return SUCCESS;
	}
	
	@Action(value = "searchMore", 
			interceptorRefs = { @InterceptorRef("sessionStack") }, 
			results = { @Result(name = "success", type = "json", params = {
					"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false"}) })
	public String searchMore() throws ErrorSqlException{
		String type=ServletActionContext.getRequest().getParameter("type");
		String searchContent=ServletActionContext.getRequest().getParameter("searchContent");
		String userId=ServletActionContext.getRequest().getParameter("userId");
		rows=new ArrayList<Map>();
		Map<String ,Object> map=new HashMap<String, Object>();
		if(!StringUtils.isBlank(searchContent)){
			if (StringUtils.equals(type, "0")) {
				map=contentService.queryNews(searchContent);
				if (Integer.parseInt(map.get("total").toString())>0) {
					rows.add(map);
				}
			}else if (StringUtils.equals(type, "1")) {
				map=contentService.queryApps(userId, searchContent);
				if (Integer.parseInt(map.get("total").toString())>0) {
					rows.add(map);
				}
			}else if(StringUtils.equals(type, "2")){
				map=contentService.queryContacts(userId, searchContent);
				if (Integer.parseInt(map.get("total").toString())>0) {
					rows.add(map);
				}
			}
			
		}
		return SUCCESS;
	}
	
	public List<Map> getRows() {
		return rows;
	}

	public int getTotal() {
		if (rows == null) {
			return 0;
		} else {
			return rows.size();
		}
	}
}
